/*
 * SpeedoGradient - Speedometer and gradient  meter application for Android 
 * Copyright 2013 Guilhem VALENTIN
 * 
 * This file is part of SpeedoGradient
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with SpeedoGradient. If not, see <http://www.gnu.org/licenses/>.
 */
package com.gvalentin.speedogradient.util;

/**
 * Rolling list of double elements
 * Introduce new methods specific to double
 * @author guilhem
 */
public class DoubleCapacityRollingList extends CapacityRollingList<Double> {
	// Total amount
	protected double total;
	
	/**
	 * Constructor
	 */
	public DoubleCapacityRollingList(int capacity) {
		super(capacity);
	}
	
	/**
	 * Overrides add method to optimize average calculation
	 * @see com.gvalentin.SpeedoGradient.util.TimeRollingList#add(java.lang.Object)
	 */
	@Override
	public synchronized boolean add(Double value) {
		total += value;
		return super.add(value);
	}

	/**
	 * Remove item value when removing
	 */
	@Override
	protected void removeHint(int index, Double value) {
		total -= value;
	}

	/**
	 * Get average of values
	 */
	public double getAverage() {
		int nb = items.size();
		if (nb > 0) {
			return (total / items.size());
		} else {
			return 0;
		}
	}
}
